﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace Expressing_Label
{
    public partial class ExpressingMiFrm : Form
    {
        private bool c_bMoving = false;
        private Point c_pStart = new Point();
        private Point[] poin = new Point[8];
        private Bitmap c_bmpFormBackground = null;
        private bool c_IsSaved = false;
        private Color _rowBackColor1 = Color.White;
        private Color _rowBackColor2 = Color.FromArgb(254, 216, 249);
        private Color _selectedColor = Color.FromArgb(102, 206, 255); 

        public ExpressingMiFrm()
        {
            InitializeComponent();
            InitializeFormBorder();
           
        }

        

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreatePolygonRgn(ref Point lpPoint, int nCount, int nPolyFillMode);

        [DllImport("user32.dll")]
        private static extern IntPtr SetWindowRgn(IntPtr hWnd, IntPtr hRgn, Boolean bReDraw);

        private void InitializeFormBorder()
        {
            poin[0] = new Point(40, 0);
            poin[1] = new Point(Width, 0);
            poin[2] = new Point(Width, Height);
            poin[3] = new Point(40, Height);
            poin[4] = new Point(40, ConfigurationFrm.g_yExpressingButton + 20);
            poin[5] = new Point(20, ConfigurationFrm.g_yExpressingButton);
            poin[6] = new Point(40, ConfigurationFrm.g_yExpressingButton - 20);
            poin[7] = new Point(40, 0);

            IntPtr hRgn = CreatePolygonRgn(ref poin[0], 16, 1);
            SetWindowRgn(Handle, hRgn, true);

            c_bmpFormBackground = new Bitmap(Width, Height);
            Graphics gra = Graphics.FromImage(c_bmpFormBackground);
            gra.Clear(Color.WhiteSmoke);
            Pen p = new Pen(Color.Purple, 4);
            for (int i = 0; i < 7; i++)
            {
                gra.DrawLine(p, poin[i], poin[i + 1]);
            }
            this.BackgroundImage = c_bmpFormBackground;
            gra.Dispose();

            for (int i = 0; i < PublicResources.g_ExpressingSourceItem.Count; i++)
            {
                if (i % 5 > 0)
                {
                    string s = PublicResources.g_ExpressingSourceItem[i];
                    string ts = s;
                    int t = Convert.ToInt32(s.Substring(s.LastIndexOf('#') + 1));
                    s = s.Substring(0, s.LastIndexOf('#'));
                    if (s.EndsWith("0"))
                    {
                        s = s.Remove(s.Length - 1);
                        Source_lstbx.Items.Add(s);
                        ts = ts.Remove(ts.IndexOf('0'), 1);
                        PublicResources.g_ExpressingSourceItem[i] = ts;
                    }
                    else
                        Show_lstbx.Items.Add(s);                   
                    PublicResources.g_ExpressingSelectedIndex.Add(s, t);

                }
            }
        }

        


        private void ExpressingMiFrm_MouseDown(object sender, MouseEventArgs e)
        {
            c_bMoving = true;
            c_pStart = MousePosition;
        }

        private void ExpressingMiFrm_MouseMove(object sender, MouseEventArgs e)
        {
            if (c_bMoving)
            {
                int x = MousePosition.X - c_pStart.X;
                int y = MousePosition.Y - c_pStart.Y;
                this.Left += x;
                this.Top += y;
                this.Owner.Left += x;
                this.Owner.Top += y;
                c_pStart = MousePosition;
            }
        }

        private void ExpressingMiFrm_MouseUp(object sender, MouseEventArgs e)
        {
            c_bMoving = false;
        }

        private void ExpressingMiFrm_MouseLeave(object sender, EventArgs e)
        {
            c_bMoving = false;
        }

        private void Right_picbx_Click(object sender, EventArgs e)
        {
            while (Source_lstbx.SelectedItems.Count > 0)
            {
                Show_lstbx.Items.Add(Source_lstbx.SelectedItems[0].ToString());
                Source_lstbx.Items.Remove(Source_lstbx.SelectedItems[0]);
            }
        }

        private void Left_picbx_Click(object sender, EventArgs e)
        {
            while (Show_lstbx.SelectedItems.Count > 0)
            {
                Source_lstbx.Items.Add(Show_lstbx.SelectedItems[0]);
                Show_lstbx.Items.Remove(Show_lstbx.SelectedItems[0]);
            }
        }

        private void ExpressingMiFrm_Load(object sender, EventArgs e)
        {
            this.Left = this.Owner.Location.X + this.Owner.Width - 40;
            this.Top = this.Owner.Location.Y;
            c_IsSaved = false;
        }


        private void Source_lstbx_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Show_lstbx.Items.Add(Source_lstbx.SelectedItem.ToString());
                Source_lstbx.Items.Remove(Source_lstbx.SelectedItem);
            }
        }

        private void Show_lstbx_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Source_lstbx.Items.Add(Show_lstbx.SelectedItem.ToString());
                Show_lstbx.Items.Remove(Show_lstbx.SelectedItem);
            }
        }

        private void OK_btn_Click(object sender, EventArgs e)
        {
            SaveToPublicResouce();

            ConfigurationFrm.g_blSendByMiFrm = true;
            ConfigurationFrm.g_iNextFrm = 3;
            if (PublicResources.g_iConfiguraState == 2)
                PublicResources.g_iConfiguraState = 3;
            this.Close();
        }

        private void SaveToPublicResouce()
        {
            PublicResources.g_ExpressingSelectedIndex.Clear();
            string s;
            for (int i = 0; i < Source_lstbx.Items.Count; i++)
            {
                s = Source_lstbx.Items[i].ToString();
                for (int j = 0; j < PublicResources.g_ExpressingSourceItem.Count; j++)
                {
                    if (PublicResources.g_ExpressingSourceItem[j].Contains(s))
                    {
                        string ts = PublicResources.g_ExpressingSourceItem[j];
                        ts = ts.Insert(ts.IndexOf('#'), "0");
                        PublicResources.g_ExpressingSourceItem[j] = ts;
                        PublicResources.g_ExpressingSelectedIndex.Remove(s);
                    }
                }
            }
            c_IsSaved = true;
        }

        private void btnSaveToFile_Click(object sender, EventArgs e)
        {
            SaveToPublicResouce();
            using (FileStream fs = new FileStream(@"ExpressingSourceItem.data", FileMode.Open, FileAccess.ReadWrite))
            {
                fs.SetLength(0);
                using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    for (int i = 0; i < PublicResources.g_ExpressingSourceItem.Count; i++)
                    {
                        if (i > 0 && i % 5 == 0)
                            sw.WriteLine("");
                        sw.WriteLine(PublicResources.g_ExpressingSourceItem[i]);
                    }
                }
            }
        }

        private void ExpressingMiFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!c_IsSaved)
                SaveToPublicResouce();
        }

        private void Show_lstbx_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
                       
            if (e.Index != ListBox.NoMatches)
            {
                Brush br = new SolidBrush(Color.WhiteSmoke);
                string s = (sender as ListBox).Items[e.Index].ToString();
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), e.Bounds);
                    e.Graphics.DrawString(s, e.Font, br, e.Bounds);
                }
                else
                {
                    Color backColor;
                    if (e.Index % 2 == 0) //偶数项  
                    {
                        backColor = _rowBackColor2;
                    }
                    else //奇数项  
                    {
                        backColor = Color.Purple;
                    }
                    using (SolidBrush brush = new SolidBrush(Color.WhiteSmoke))
                    {
                        e.Graphics.FillRectangle(brush, e.Bounds);
                    }
                    e.Graphics.DrawString(s, e.Font, new SolidBrush(Color.Blue), e.Bounds);
                }

                
            }
        }
    }
}
